<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" version="4.0" media-type="text/html"/>
  <xsl:strip-space elements="*" />

  <xsl:variable name="pageTitle">Movie List</xsl:variable>
  <xsl:variable name="nrOfColumns">2</xsl:variable>
  <xsl:variable name="nrOfRows">5</xsl:variable>
  <xsl:variable name="coverWidth">95</xsl:variable>
  <xsl:variable name="coverHeight">140</xsl:variable>
  <xsl:variable name="nrOfGenres">3</xsl:variable>
  <xsl:variable name="nrOfActors">4</xsl:variable>
  <xsl:variable name="nrOfDirectors">2</xsl:variable>
  <xsl:variable name="maxtitlechars">30</xsl:variable>
  <xsl:variable name="maxotherschars">40</xsl:variable>
  <xsl:variable name="maxdirectorchars">25</xsl:variable>
  <xsl:variable name="maxcharacterchars">20</xsl:variable>

  <!-- Auxiliary variables. -->
  <xsl:variable name="nrOfMoviesParPage">
    <xsl:number value="$nrOfColumns * $nrOfRows"/>
  </xsl:variable>

  <!-- Auxiliary method to display a page index and the total number of pages. -->
  <xsl:template name="formatPageIndex">
    <xsl:param name="pageIndex">1</xsl:param>
    <xsl:variable name="nrOfPages">
      <xsl:number value="ceiling(count(//movieInfo) div number($nrOfMoviesParPage))"/>
    </xsl:variable>
    <xsl:choose>
      <xsl:when test="$pageIndex > 0">
        <xsl:value-of select="ceiling(number($pageIndex))"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text>1</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
    <xsl:text>/</xsl:text>
    <xsl:value-of select="$nrOfPages"/>
  </xsl:template>

  <!-- Auxiliary method to display a page header. -->
  <xsl:template name="formatPageHeader">
    <xsl:param name="insertPageBreak" select="false"/>
    <xsl:variable name="pageIndex">
      <xsl:value-of select="position() div number($nrOfMoviesParPage)"/>
    </xsl:variable>
    <xsl:if test="$pageIndex > 1">
      <xsl:text disable-output-escaping="yes">&lt;/TABLE&gt;</xsl:text>
    </xsl:if>
    <xsl:if test="normalize-space($insertPageBreak)='true'">
      <P STYLE="page-break-before: always; margin-left: 0px; margin-right: 0px; margin-top: 0px; margin-bottom: 0 px"></P>
    </xsl:if>
    <TABLE WIDTH="517">
      <TR>
        <TD ALIGN="left" WIDTH="33%" height="20" valign="bottom" class="pagetitle">
        </TD>
        <TD ALIGN="center" WIDTH="33%" height="40" valign="bottom" class="pagetitle">
          <xsl:choose>
            <xsl:when test="$pageTitle!=''">
              <xsl:value-of select="$pageTitle"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:text>Movie List</xsl:text>
            </xsl:otherwise>
          </xsl:choose>
        </TD>
        <TD ALIGN="right" WIDTH="33%" height="40" valign="bottom" class="pagetitle">
          <xsl:call-template name="formatPageIndex">
            <xsl:with-param name="pageIndex" select="$pageIndex"/>
          </xsl:call-template>
        </TD>
      </TR>
    </TABLE>
  </xsl:template>

  <!-- Auxiliary method to display the (front) cover if available. -->
  <xsl:template name="formatCover">
    <xsl:choose>
      <xsl:when test="picture!=''">
        <TD WIDTH="97" class="picture">
          <IMG SRC="{picture}" BORDER="0" height="{$coverHeight}"></IMG>
        </TD>
      </xsl:when>
      <xsl:otherwise>
        <TD width="97" class="picture">
          <IMG SRC="nocover.gif" BORDER="0">
            <xsl:attribute name="HEIGHT">
              <xsl:value-of select="$coverHeight"/>
            </xsl:attribute>
            <xsl:attribute name="WIDTH">
              <xsl:value-of select="$coverWidth"/>
            </xsl:attribute>
          </IMG>
        </TD>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- Auxiliary method to display the genres for the movie. -->
  <xsl:template name="formatGenre">
    <xsl:for-each select="genres/genre">
      <xsl:if test="position() &lt;= $nrOfGenres">
        <xsl:value-of select="node()"/>
        <xsl:if test="(position()!=last()) and (position() &lt; $nrOfGenres)">
          <xsl:text>, </xsl:text>
        </xsl:if>
      </xsl:if>
    </xsl:for-each>
  </xsl:template>

  <!-- Auxiliary method to display a limited set of actors for the movie. -->
  <xsl:template name="formatActors">
    <table cellpadding='0' cellspacing='0'>
      <xsl:for-each select="actors/actor">
        <xsl:if test="position() &lt;= $nrOfActors">
          <tr>
            <td class="actor">
              <xsl:value-of select="node()"/>
            </td>
          </tr>
        </xsl:if>
      </xsl:for-each>
    </table>
  </xsl:template>

  <!-- Auxiliary method to display other info for the movie. -->
  <xsl:template name="formatOthers">
    <xsl:if test="mpaaRating!=''">
      <xsl:choose>
        <xsl:when test="string-length(mpaarating)&gt;$maxotherschars">
          <xsl:value-of select="substring(mpaaRating,1,$maxotherschars)"/>
          <xsl:text>...</xsl:text>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="mpaaRating"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:if>
  </xsl:template>



  <!-- Auxiliary method to set the stars in the rating field. -->
  <xsl:template name="star">
    <xsl:param name="num" select="0"/>
    <xsl:choose>
      <xsl:when test="$num=1"><![CDATA[&frac12;]]></xsl:when>
      <xsl:when test="$num>1">
        <![CDATA[<b>&times;</b>]]><xsl:call-template name="star">
          <xsl:with-param name="num" select="$num - 2"/>
        </xsl:call-template>
      </xsl:when>
    </xsl:choose>
  </xsl:template>
  <!-- Auxiliary method to set the stars in the rating field. -->

  <xsl:template name="addEmptyCells">
    <xsl:param name="num" select="0"/>
    <xsl:choose>
      <xsl:when test="$num=1">
        <TD>
          <xsl:variable name="widthFloat">
            <xsl:value-of select="1 div number($nrOfColumns)"/>
          </xsl:variable>
          <xsl:attribute name="WIDTH">
            <xsl:value-of select='format-number($widthFloat, "##.#%")'/>
          </xsl:attribute>
          <xsl:text> </xsl:text>
        </TD>
      </xsl:when>
      <xsl:when test="$num > 1">
        <TD>
          <xsl:text> </xsl:text>
        </TD>
        <xsl:call-template name="addEmptyCells">
          <xsl:with-param name="num" select="$num - 1"/>
        </xsl:call-template>
      </xsl:when>
    </xsl:choose>
  </xsl:template>

  <!-- Auxiliary method to display the basic information for one movie. -->
  <xsl:template name="formatMovie">
    <xsl:param name="indexNr" select="1"/>
    <TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" width="286">
      <TR>
        <TD CLASS="subtitle" height="20">
          <xsl:value-of select="$indexNr"/>
          <xsl:text>.</xsl:text>
          <xsl:choose>
            <xsl:when test="string-length(title)&gt;$maxtitlechars">
              <xsl:value-of select="substring(title,1,$maxtitlechars)"/>
              <xsl:text>..</xsl:text>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="title"/>
            </xsl:otherwise>
          </xsl:choose>

          <xsl:if test="year!=''">
            <xsl:text> (</xsl:text><xsl:value-of select="year"/>)
          </xsl:if>
        </TD>
        <TD ALIGN="right" width="1" class="rating">
          <xsl:if test="myrating!='0'">
            <xsl:call-template name="star">
              <xsl:with-param name="num" select="myrating"/>
            </xsl:call-template>
          </xsl:if>
        </TD>
      </TR>
      <TR>
        <TD COLSPAN="2">
          <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0">
            <TR VALIGN="top">
              <xsl:call-template name="formatCover"/>
              <TD VALIGN="top" width="100%">
                <TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" width="100%">
                  <TR>
                    <TD ALIGN="left" class="genre" height="18">
                      <xsl:call-template name="formatGenre"/>
                    </TD>
                  </TR>
                  <TR>
                    <TD ALIGN="left" class="director" height="18">
                      <xsl:value-of select="director"/>
                    </TD>
                  </TR>
                  <TR>
                    <TD ALIGN="left" valign="top" height="58" class="actors">
                      <xsl:call-template name="formatActors"/>
                    </TD>
                  </TR>
                  <TR>
                    <TD ALIGN="left" valign="top" class="others" height="28">
                      <xsl:call-template name="formatOthers"/>
                    </TD>
                  </TR>
                  <TR>
                    <TD ALIGN="left" class="runtime" height="18">
                      <xsl:value-of select="runtime"/>
                      <xsl:text> </xsl:text>
                      <xsl:value-of select="/movies/metadata/field[@name='runtimeUnit']/@label"/>
                    </TD>
                  </TR>
                </TABLE>
              </TD>
            </TR>
          </TABLE>
        </TD>
      </TR>
    </TABLE>
  </xsl:template>

  <!-- The main template. -->
  <xsl:template match="/">
    <xsl:variable name="nrOfPages">
      <xsl:number value="ceiling(count(//movie) div number($nrOfMoviesParPage))"/>
    </xsl:variable>
    <HTML>
      <HEAD>
        <xsl:choose>
          <xsl:when test="$pageTitle!=''">
            <TITLE>
              <xsl:value-of select="$pageTitle"/>
            </TITLE>
          </xsl:when>
          <xsl:otherwise>
            <TITLE>Movie List</TITLE>
          </xsl:otherwise>
        </xsl:choose>
        <LINK REL="stylesheet" HREF="print_item_table.css" TYPE="text/css"/>
      </HEAD>
      <BODY>
        <!-- Add a page number for the first page. -->
        <xsl:call-template name="formatPageHeader">
          <xsl:with-param name="insertPageBreak" select="false()"/>
        </xsl:call-template>
        <TABLE border="0" width="517">
          <xsl:apply-templates/>
        </TABLE>
      </BODY>
    </HTML>
  </xsl:template>

  <!-- Template for adding each movie to the movie list. -->
  <xsl:template match="movieInfo">
    <!-- Start a new row if necessary & insert a page break if the wanted number  -->
    <!-- of rows was constructed for the previous page. -->
    <xsl:variable name="indexNr">
      <xsl:number value="position()"/>
    </xsl:variable>
    <xsl:if test="($indexNr mod $nrOfColumns) = 1">
      <xsl:choose>
        <xsl:when test="(((position()-1) mod ($nrOfMoviesParPage)) = 0) and (position() > 1)">
          <!-- Add a page number for this page. -->
          <xsl:call-template name="formatPageHeader">
            <xsl:with-param name="insertPageBreak" select="true()"/>
          </xsl:call-template>
          <xsl:text disable-output-escaping="yes">&lt;TABLE&gt; &lt;TR VALIGN="top"&gt;</xsl:text>
        </xsl:when>
        <xsl:otherwise>
          <xsl:text disable-output-escaping="yes">&lt;TR VALIGN="top"&gt;</xsl:text>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:if>

    <!-- Process the curent movie and add it to the constructed table layout. -->
    <TD>
      <xsl:variable name="widthFloat">
        <xsl:value-of select="1 div number($nrOfColumns)"/>
      </xsl:variable>
      <xsl:attribute name="WIDTH">
        <xsl:value-of select='format-number($widthFloat, "##.#%")'/>
      </xsl:attribute>
      <xsl:call-template name="formatMovie">
        <xsl:with-param name="indexNr" select="$indexNr"/>
      </xsl:call-template>
    </TD>

    <!-- End the curent row if necessary. -->
    <xsl:if test="(position() mod $nrOfColumns) = 0">
      <xsl:text disable-output-escaping="yes">&lt;/TR&gt;</xsl:text>
    </xsl:if>

    <!-- Fill the last row with empty cells if necessary. -->
    <xsl:if test="(position()=last()) and ((position() mod $nrOfColumns) != 0)">
      <xsl:call-template name="addEmptyCells">
        <xsl:with-param name="num" select="position() mod $nrOfColumns"/>
      </xsl:call-template>
      <xsl:text disable-output-escaping="yes">&lt;/TR&gt;</xsl:text>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>
